<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-02-13
 * Time: 22:16
 */
namespace app\admin\controller;
use app\admin\model\Tool;
use app\admin\model\ToolApp;
use think\Controller;
use think\Db;
class AdminToolApp extends Controller
{
    //中间件登录限制
    protected $middleware = [
        'AdminCheck'
    ];
    //预约申请
    public function tool_app_list(){
        $tool_app=new ToolApp();
        $tool_apps=$tool_app
            ->where('statu','<=',1)
            ->paginate(10);
        $this->assign('tools',$tool_apps);
        return $this->fetch();
    }
    //历史记录
    public function tool_history_list(){
        $tool_app=new ToolApp();
        $tool_apps=$tool_app
            ->where('statu','>',1)
            ->order('create_time','desc')
            ->paginate(10);
        $this->assign('tools',$tool_apps);
        return $this->fetch();
    }
    //教师预约申请
    public function tool_teacher_list(){
        $teacher_id=\session('teacher')['id'];
        $tool_id=Tool::hasWhere('lab',[
            'teacher_id'=>$teacher_id
        ])->select()->column('id');
        $tool_app=new ToolApp();
        $tool_apps=$tool_app
            ->where('tool_id','in',$tool_id)
            ->where('statu','<=',1)
            ->paginate(10);
        $this->assign('tools',$tool_apps);
        return $this->fetch();
    }
    //教师历史预约记录
    public function tool_teacher_history_list(){
        $teacher_id=\session('teacher')['id'];
        $tool_id=Tool::hasWhere('lab',[
            'teacher_id'=>$teacher_id
        ])->select()->column('id');
        $tool_app=new ToolApp();
        $tool_apps=$tool_app
            ->where('tool_id','in',$tool_id)
            ->where('statu','>',1)
            ->order('create_time','desc')
            ->paginate(10);
        $this->assign('tools',$tool_apps);
        return $this->fetch();
    }
    //审核
    public function tool_shenhe(){
        $data=input('post.');
//        return $data['id'];
        $tool_app=ToolApp::get($data['id']);
        switch ($data['statu']){
            case 1:
                $tool=Db::table('tool')->where('id',$tool_app['tool_id'])->find();
                if($tool['statu']==1){
                    $msg=$this->msg(400,'此器具已被借出，请选择退回');
                }else{
                    $tool_app->statu =1;
                    $tool_app->tool->statu=1;
                    Db::table('tool')->where('id',$tool_app['tool_id'])->setInc('times');
                    $tool_app->together('tool')->save();
                    $msg=$this->msg(200,'审核成功');
                }
                break;
            case 2:
                $tool_app->statu=2;
                $tool_app->reason=$data['reason'];
                $tool_app->save();
                $msg=$this->msg(200,'审核成功');
                break;
            case 3:
                $tool_app->statu =3;
                $tool_app->tool->statu=0;
                $tool_app->together('tool')->save();
                $msg=$this->msg(200,'审核成功');
                break;
            default:
                $msg=$this->msg(400,'错误');
        }
        return $msg;

//        return $data;
    }
    //信息
    public function msg($statu,$msg){
        return [
            'statu'=>$statu,
            'msg'  =>$msg
        ];
    }
}